package com.example.demo.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.*;
import com.example.demo.mapper.ActivityWhoJoinMapper;
import com.example.demo.mapper.BusinessMapper;
import com.example.demo.mapper.GoodsMapper;
import com.example.demo.mapper.PrizeMapper;
import com.example.demo.service.GoodsService;
import io.swagger.models.auth.In;
import org.aspectj.weaver.ast.Or;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.annotation.Resource;
import java.util.*;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private BusinessMapper businessMapper;
    public int addCommodity(Goods good){
        return goodsMapper.insert(good);
    }

    public int delCommodity(MyArray goodId){
        List<Integer> list=new ArrayList<>();
        for(int id:goodId.getIds()){
            list.add(id);
        }
        return goodsMapper.deleteBatchIds(list);
    }


    public List<OrganizerGoods> getCommodities(){
        List<Goods> goods = goodsMapper.selectList(null);
        List<OrganizerGoods> goodsList = new ArrayList<>();
        for(Goods goods1 : goods){
            Business business = businessMapper.selectById(goods1.getBusinessId());
            OrganizerGoods organizerGoods = new OrganizerGoods(goods1,business.getBusinessName());
            goodsList.add(organizerGoods);
        }
        return goodsList;
    }

    @Resource
    private PrizeMapper prizeMapper;
    @Resource
    private ActivityWhoJoinMapper activityWhoJoinMapper;
    public List<OrganizerGoods> getCommoditiesUnChoose(Integer activityId){
        QueryWrapper<ActivityWhoJoin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activity_id",activityId);
        List<ActivityWhoJoin> businessWhoJoin = activityWhoJoinMapper.selectList(queryWrapper);
        //获取参与了该活动的商家

        List<OrganizerGoods> goodsList = new ArrayList<>();
        for(ActivityWhoJoin activityWhoJoin : businessWhoJoin){
            QueryWrapper<Goods> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("business_id",activityWhoJoin.getBusinessId());
            List<Goods> businessGoodList = goodsMapper.selectList(queryWrapper1);
            QueryWrapper<Prize> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("activity_id",activityId);
            //循环的获取到每个商家的所有商品
            List<Prize> prizeList = prizeMapper.selectList(queryWrapper2);
            List<Integer> goodIds=new ArrayList<>();
            for(Prize prize:prizeList){
                if(!goodIds.contains(prize.getGoodId())){
                    goodIds.add(prize.getGoodId());
                }
            }
            for(Goods goods : businessGoodList){
                if(!goodIds.contains(goods.getGoodId())){
                    goodsList.add(new OrganizerGoods(goods, businessMapper.selectById(goods.getBusinessId()).getBusinessName()));
                }
//                for(Prize prize : prizeList){
//                    if(goods.getGoodId() == prize.getGoodId() && activityId == prize.getActivityId()){
//                        continue;
//                    }else {
//                        goodsList.add(new OrganizerGoods(goods, businessMapper.selectById(goods.getBusinessId()).getBusinessName()));
//                    }
//                }
            }
        }
        return goodsList;
    }
    public Map getCommodities(String name,int size,int current,int businessId){
        Map <String,Object> map=new HashMap<>();
        QueryWrapper<Goods> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("business_id",businessId);
        IPage<Goods> page=new Page<>();
        page.setSize(size);
        page.setCurrent(current);
        if(name==null){
            goodsMapper.selectPage(page,null);
        }else if(!name.equals("null")){
            queryWrapper.like("good_name",name);
            goodsMapper.selectPage(page,queryWrapper);
        }
        map.put("list",page.getRecords());
        map.put("size",page.getSize());
        map.put("current",page.getCurrent());
        map.put("total",page.getTotal());
        return map;
    }

    public Goods getGoodById(Integer goodId){
        Goods good=goodsMapper.selectById(goodId);
        if(good!=null){
            return good;
        }else{
            return null;
        }
    }

    public List<Goods> getCommodity(int businessId){
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id", businessId);
        List<Goods> res = goodsMapper.selectList(queryWrapper);
        if(res != null){
            return res;
        }else{
            return null;
        }
    }

    public int editCommodity(Goods good){
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id",good.getBusinessId());
        List<Goods> res = goodsMapper.selectList(queryWrapper);
        if(res != null){
            UpdateWrapper<Goods> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("good_id", good.getGoodId());
            updateWrapper.set("good_stock", good.getGoodStock());
            updateWrapper.set("good_name", good.getGoodName());
            updateWrapper.set("good_price", good.getGoodPrice());
            updateWrapper.set("good_image", good.getGoodImage());
            goodsMapper.update(null,updateWrapper);
            return 1;
        }
       return 0;
    }

}
